package com.hand.demo.api.controller.v1;

import org.hzero.core.util.Results;
import org.hzero.core.base.BaseController;
import com.hand.demo.domain.entity.LmExample;
import com.hand.demo.domain.repository.LmExampleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import com.hand.demo.app.service.LmExampleService;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import io.swagger.annotations.ApiParam;
import java.util.List;

/**
 *  管理 API
 *
 * @author xiuhong.chen@hand-china.com 2023-09-23 11:33:24
 */
@RestController("lmExampleSiteController.v1")
@RequestMapping("/v1/lm-examples")
public class LmExampleSiteController extends BaseController {

    @Autowired
    private LmExampleRepository lmExampleRepository;
    @Autowired
    private LmExampleService lmExampleService;

    @ApiOperation(value = "维护-分页查询列表")
    @Permission(level = ResourceLevel.SITE)
    @GetMapping
    public ResponseEntity<Page<LmExample>> page(LmExample lmExample, @ApiIgnore @SortDefault(value = LmExample.FIELD_ID,
            direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<LmExample> list = lmExampleRepository.pageAndSort(pageRequest, lmExample);
        return Results.success(list);
    }

    @ApiOperation(value = "维护-查询明细")
    @Permission(level = ResourceLevel.SITE)
    @GetMapping("/{id}")
    public ResponseEntity<LmExample> detail(@ApiParam(value = "ID", required = true) @PathVariable Long id) {
        LmExample lmExample = lmExampleRepository.selectByPrimaryKey(id);
        return Results.success(lmExample);
    }

    @ApiOperation(value = "维护-创建")
    @Permission(level = ResourceLevel.SITE)
    @PostMapping
    public ResponseEntity<LmExample> create(@RequestBody LmExample lmExample) {
        validObject(lmExample);
        lmExampleService.save(lmExample);
        return Results.success(lmExample);
    }

    @ApiOperation(value = "维护-修改")
    @Permission(level = ResourceLevel.SITE)
    @PutMapping
    public ResponseEntity<LmExample> update(@RequestBody LmExample lmExample) {
        SecurityTokenHelper.validToken(lmExample);
        lmExampleService.save(lmExample);
        return Results.success(lmExample);
    }

    @ApiOperation(value = "维护-批量保存")
    @Permission(level = ResourceLevel.SITE)
    @PostMapping("/batch-saving")
    public ResponseEntity<List<LmExample>> batchSave(@RequestBody List<LmExample> lmExampleList) {
        SecurityTokenHelper.validToken(lmExampleList);
        lmExampleService.batchSave(lmExampleList);
        return Results.success(lmExampleList);
    }

    @ApiOperation(value = "维护-删除")
    @Permission(level = ResourceLevel.SITE)
    @DeleteMapping
    public ResponseEntity<Void> remove(@RequestBody LmExample lmExample) {
        SecurityTokenHelper.validToken(lmExample);
        lmExampleRepository.deleteByPrimaryKey(lmExample);
        return Results.success();
    }

}
